/**
 * 
 */
package kr.co.insoft.auth.security;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import org.springframework.security.core.Authentication;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
public class AuthenticationSerializer {
	public byte[] serializeToByteArray(final Authentication object) {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		try (ObjectOutput outputStream = new ObjectOutputStream(byteArrayOutputStream)) {
			outputStream.writeObject(object);
			return byteArrayOutputStream.toByteArray();
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		}
	}

	public Authentication deserializeFrom(final byte[] bytes) {
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
		try (ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream)) {
			return (Authentication) inputStream.readObject();
		} catch (ClassNotFoundException | IOException e) {
			throw new RuntimeException(e);
		}
	}
}
